<template>
  <div id="tpr-list" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb">
        <li class="breadcrumb-item ms-1">
          <router-link to="/h5/tools/tprs">出票数据</router-link>
        </li>
        <span class="ms-auto me-2" @click.stop="back()">返回</span>
      </ol>
    </nav>

    <template v-if="isSearchMode">
      <div class="card-body">
        <form class="">
          <my-date-picker
            :id="'beginDate'"
            :class="'form-control m-1'"
            :placeholder="'开始日期'"
            v-model="beginDate"
          ></my-date-picker>
          <my-date-picker
            :id="'endDate'"
            :class="'form-control m-1'"
            :placeholder="'截止日期'"
            v-model="endDate"
          ></my-date-picker>
          <input
            class="form-control m-1"
            type="text"
            placeholder="eterm用户名"
            size="10"
            v-model="etermUsername"
          />
          <input
            class="form-control m-1"
            type="text"
            placeholder="设备号"
            size="4"
            v-model="deviceId"
          />
          <input
            class="form-control m-1"
            type="text"
            placeholder="票号"
            size="12"
            v-model.trim="ticketNo"
          />
          <button
            type="button"
            class="btn btn-success me-2"
            @click.stop="search()"
          >
            确定
          </button>
          <button
            type="button"
            class="btn btn-info btn-sm"
            @click.stop="reset()"
          >
            重置
          </button>
        </form>
      </div>
    </template>
    <template v-else>
      <div class="card-body py-1">
        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearch()"
        >
          查找
        </button>
      </div>
      <table class="table table-sm table-striped table-hover">
        <thead>
          <tr>
            <th>日期</th>
            <th class="d-none d-md-table-cell">设备号</th>
            <th class="d-none d-md-table-cell">票号</th>
            <th>编码</th>
            <th>出发</th>
            <th>到达</th>
            <th class="text-end">价格</th>
            <th class="text-end">税</th>
            <th class="text-end">佣金</th>
          </tr>
        </thead>
        <tbody>
          <template v-for="flight in dataList">
            <tr :key="flight.id + `0`">
              <td>{{ flight.sellDate.substring(5) }}</td>
              <td class="d-none d-md-table-cell">
                <small>{{ flight.deviceId }}</small>
              </td>
              <td class="d-none d-md-table-cell">
                {{ flight.ticketNo }}
                <span class="text-muted" v-if="flight.ticketStatus === 1"
                  >改签</span
                >
                <span class="text-muted" v-if="flight.ticketStatus === 4"
                  >退票</span
                >
                <span class="text-muted" v-if="flight.ticketStatus === 16"
                  >废</span
                >
                {{ flight.conjTag }}
              </td>
              <td>
                {{ flight.pnrNo }}
              </td>
              <td>{{ flight.orig }}</td>
              <td>{{ flight.dest }}</td>
              <td class="text-end">{{ flight.price }}</td>
              <td class="text-end">{{ flight.tax }}</td>
              <td class="text-end">{{ flight.commission }}</td>
            </tr>
            <tr :key="flight.id + `1`" class="bg-info text-white">
              <td></td>
              <td class="d-none d-md-table-cell"></td>
              <td colspan="6">
                {{ flight.ticketNo }}
                <span class="text-muted" v-if="flight.ticketStatus === 1"
                  >改签</span
                >
                <span class="text-muted" v-if="flight.ticketStatus === 4"
                  >退票</span
                >
                <span class="text-muted" v-if="flight.ticketStatus === 16"
                  >废</span
                >
                {{ flight.conjTag }}
              </td>
            </tr>
          </template>
        </tbody>
      </table>
      <div class="card-block">
        <my-pagination
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </div>
    </template>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination_h5.vue";
import MyDatePicker from "@/components/my-datepicker.vue";
import { searchTpr, saveTprContent } from "@/api/flight.js";
import { searchSuppliers } from "@/api/basic-data.js";
import { APP_FLIGHT_PATH } from "@/common/common.js";
import $ from "jquery";

export default {
  name: "TprList",
  components: {
    MyPagination,
    MyDatePicker,
  },
  data() {
    return {
      isSearchMode: false,
      dataList: [],
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      beginDate: "",
      endDate: "",
      deviceId: "",
      ticketNo: "",
      etermUsername: "",

      tprContent: "",
      suppliers: [],

      uploading: false,
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },
    showSearch: function () {
      this.isSearchMode = true;
    },
    search: function () {
      let deviceId = this.deviceId;
      if (deviceId === "") {
        deviceId = 0;
      }

      let beginDate = this.beginDate;
      if (beginDate === "") {
        beginDate = null;
      }

      let endDate = this.endDate;
      if (endDate === "") {
        endDate = null;
      }

      this.isSearchMode = false;

      let params = {
        beginDate: beginDate,
        endDate: endDate,
        device: deviceId,
        ticketNo: this.ticketNo,
        etermUsername: this.etermUsername,
        pageSize: this.sc.pageSize,
        pageNo: this.sc.pageNo,
      };

      this.showLoading();

      searchTpr(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    reset: function () {
      this.beginDate = "";
      this.endDate = "";
      this.deviceId = "";
      this.ticketNo = "";
      this.etermUsername = "";
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    showAbbr: function (val) {
      if (val !== null && val.length > 10) {
        return val.substring(0, 10) + "...";
      } else {
        return val;
      }
    },
    showStatus: function (val) {
      switch (val) {
        case 0:
          return "";
        case 4:
          return "已退票";
        default:
          return val;
      }
    },
  },
};
</script>
